package com.dmj.sqldsl.executor.visitor;

import com.dmj.sqldsl.model.Limit;
import com.dmj.sqldsl.model.table.SimpleTable;
import com.dmj.sqldsl.model.table.SubQueryTable;

public class OracleQueryModelVisitor extends QueryModelVisitor {

  private static final String PAGE_COLUMN_NAME = "rownum";

  @Override
  protected String visit(SimpleTable table) {
    return String.format("%s %s", table.getTableName(), getRealAlias(table.getTableId()));
  }

  @Override
  protected String visit(SubQueryTable table) {
    return String.format("(%s) %s", visit(table.getQuery()), getRealAlias(table.getTableId()));
  }

  @Override
  protected String visitLimit(String allSqlWithoutLimit, Limit limit) {
    params.add(new Parameter(limit.getOffset()));
    params.add(new Parameter(limit.getSize() + limit.getOffset()));
    return String.format("select m.* from (%s) m where %s between ? and ?",
        allSqlWithoutLimit,
        PAGE_COLUMN_NAME);
  }
}
